class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ # 方法一 遍历两次数组 l = len(nums) for i in range(l-1): for j in range(i+1,l): if nums[i] + nums[j] == target: return i,j
if __name__ == "__main__": n = [3, 2, 4, 11, 15] t = 6 s = Solution() print(s.twoSum(n, t))
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ # 方法二 遍历一次数组 l = range(len(nums)) for i in l: temp = target - nums[i] if temp in nums and nums.index(temp) != i: return i, nums.index(temp)
if __name__ == "__main__": n = [3, 2, 4, 11, 15] t = 6 s = Solution() print(s.twoSum(n, t))
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ # 方法三 使用字典 d = {} for i, num in enumerate(nums): print(i,num) if target - num in d: return [d[target - num], i] d[num] = i
if __name__ == "__main__": n = [3, 2, 4, 11, 15] t = 6 s = Solution() print(s.twoSum(n, t))